<template>
  <div class="recent-activities">
    <div class="activities-header">
      <h3 class="title">最近活动</h3>
      <span v-if="loading" class="loading">加载中...</span>
    </div>
    
    <div class="activities-content" v-if="activities.length > 0">
      <div class="timeline">
        <div v-for="(activity, index) in activities" :key="activity.id" class="timeline-item" :class="getActivityTypeClass(activity.type)">
          <div class="timeline-badge">
            <i class="fas" :class="getActivityTypeIcon(activity.type)"></i>
          </div>
          <div class="timeline-panel">
            <div class="timeline-header">
              <span class="time">{{ formatTime(activity.time) }}</span>
              <span class="activity-type">{{ activity.type }}</span>
              <span class="activity-status" :class="getStatusClass(activity.status)">{{ activity.status }}</span>
            </div>
            <div class="timeline-body">
              <p class="content">{{ activity.content }}</p>
              <div class="user-info" v-if="activity.user">
                <img :src="activity.user.avatar" alt="用户头像" class="user-avatar" />
                <span class="user-name">{{ activity.user.name }}</span>
                <span class="user-role">{{ activity.user.role }}</span>
              </div>
            </div>
            <div class="timeline-footer" v-if="activity.needsAction">
              <button class="action-button">处理</button>
            </div>
          </div>
        </div>
      </div>
      
      <div class="view-more-container">
        <button class="view-more-button" @click="loadMore" :disabled="loading">
          <span v-if="!loading">查看更多</span>
          <span v-else>加载中...</span>
        </button>
      </div>
    </div>
    
    <div class="empty-state" v-else-if="!loading">
      <i class="fas fa-history empty-icon"></i>
      <p>暂无活动记录</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { generateRecentActivitiesData } from '@/utils/adminMock';

interface ActivityUser {
  id: string;
  name: string;
  role: string;
  avatar: string;
}

interface Activity {
  id: string;
  type: string;
  time: string;
  timestamp: number;
  content: string;
  user: ActivityUser;
  status: string;
  needsAction: boolean;
}

const activities = ref<Activity[]>([]);
const loading = ref(false);
const page = ref(1);
const pageSize = ref(5);

// 加载活动数据
const loadActivities = () => {
  loading.value = true;
  setTimeout(() => {
    // 使用模拟数据，实际项目中这里应该调用API
    const newActivities = generateRecentActivitiesData(pageSize.value);
    
    if (page.value === 1) {
      activities.value = newActivities;
    } else {
      activities.value = [...activities.value, ...newActivities];
    }
    
    loading.value = false;
  }, 500); // 模拟网络延迟
};

// 加载更多活动
const loadMore = () => {
  page.value += 1;
  loadActivities();
};

// 根据活动类型获取图标
const getActivityTypeIcon = (type: string): string => {
  const iconMap: {[key: string]: string} = {
    '用户注册': 'fa-user-plus',
    '订单创建': 'fa-file-invoice',
    '订单支付': 'fa-credit-card',
    '订单发货': 'fa-truck',
    '订单完成': 'fa-check-circle',
    '提款申请': 'fa-money-bill-wave',
    '提款审核': 'fa-tasks',
    '提款完成': 'fa-check-double',
    '产品上架': 'fa-box',
    '产品下架': 'fa-archive',
    '代理审核': 'fa-user-shield',
    '经销商审核': 'fa-store',
    '系统公告': 'fa-bullhorn',
    '佣金结算': 'fa-money-check-alt'
  };
  
  return iconMap[type] || 'fa-history';
};

// 根据活动类型获取CSS类
const getActivityTypeClass = (type: string): string => {
  const classMap: {[key: string]: string} = {
    '用户注册': 'type-user',
    '订单创建': 'type-order',
    '订单支付': 'type-payment',
    '订单发货': 'type-order',
    '订单完成': 'type-order',
    '提款申请': 'type-withdraw',
    '提款审核': 'type-withdraw',
    '提款完成': 'type-withdraw',
    '产品上架': 'type-product',
    '产品下架': 'type-product',
    '代理审核': 'type-agent',
    '经销商审核': 'type-distributor',
    '系统公告': 'type-system',
    '佣金结算': 'type-commission'
  };
  
  return classMap[type] || '';
};

// 根据状态获取CSS类
const getStatusClass = (status: string): string => {
  const classMap: {[key: string]: string} = {
    '成功': 'status-success',
    '待支付': 'status-pending',
    '已支付': 'status-success',
    '待发货': 'status-pending',
    '已发货': 'status-processing',
    '已完成': 'status-success',
    '已取消': 'status-canceled',
    '待审核': 'status-pending',
    '已上架': 'status-success',
    '已下架': 'status-canceled',
    '已通过': 'status-success',
    '已拒绝': 'status-canceled',
    '已发布': 'status-success',
    '已结算': 'status-success'
  };
  
  return classMap[status] || '';
};

// 格式化时间显示
const formatTime = (timeStr: string): string => {
  const date = new Date(timeStr);
  const now = new Date();
  const diffMs = now.getTime() - date.getTime();
  const diffSec = Math.floor(diffMs / 1000);
  const diffMin = Math.floor(diffSec / 60);
  const diffHour = Math.floor(diffMin / 60);
  const diffDay = Math.floor(diffHour / 24);
  
  if (diffSec < 60) {
    return '刚刚';
  } else if (diffMin < 60) {
    return `${diffMin}分钟前`;
  } else if (diffHour < 24) {
    return `${diffHour}小时前`;
  } else if (diffDay < 30) {
    return `${diffDay}天前`;
  } else {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
  }
};

// 组件加载时获取活动数据
onMounted(() => {
  loadActivities();
});
</script>

<style scoped>
.recent-activities {
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
  padding: 20px;
}

.activities-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.loading {
  color: #888;
  font-size: 14px;
}

.timeline {
  position: relative;
  padding: 0;
  list-style: none;
}

.timeline:before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 24px;
  width: 3px;
  background-color: #e9ecef;
  z-index: 0;
}

.timeline-item {
  position: relative;
  margin-bottom: 20px;
  padding-left: 50px;
}

.timeline-item:last-child {
  margin-bottom: 0;
}

.timeline-badge {
  position: absolute;
  top: 0;
  left: 0;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  text-align: center;
  line-height: 50px;
  background-color: #5e72e4;
  color: #fff;
  z-index: 1;
}

.timeline-badge i {
  font-size: 20px;
}

.timeline-panel {
  position: relative;
  background-color: #f8f9fa;
  border-radius: 8px;
  padding: 15px;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.05);
}

.timeline-panel:before {
  content: '';
  position: absolute;
  top: 15px;
  left: -8px;
  border-top: 8px solid transparent;
  border-right: 8px solid #f8f9fa;
  border-bottom: 8px solid transparent;
}

.timeline-header {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.time {
  color: #888;
  font-size: 12px;
  margin-right: 10px;
}

.activity-type {
  background-color: #e9ecef;
  color: #666;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  margin-right: 10px;
}

.activity-status {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.status-success {
  background-color: rgba(45, 206, 137, 0.1);
  color: #2dce89;
}

.status-pending {
  background-color: rgba(251, 175, 66, 0.1);
  color: #fbaf42;
}

.status-processing {
  background-color: rgba(94, 114, 228, 0.1);
  color: #5e72e4;
}

.status-canceled {
  background-color: rgba(245, 54, 92, 0.1);
  color: #f5365c;
}

.content {
  margin: 0 0 10px 0;
  color: #333;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  margin-right: 8px;
}

.user-name {
  font-weight: 500;
  margin-right: 8px;
  color: #444;
}

.user-role {
  background-color: rgba(94, 114, 228, 0.1);
  color: #5e72e4;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;
}

.timeline-footer {
  margin-top: 10px;
  display: flex;
  justify-content: flex-end;
}

.action-button {
  background-color: #5e72e4;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.action-button:hover {
  background-color: #4a5cbc;
}

.view-more-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.view-more-button {
  background-color: transparent;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 8px 16px;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
}

.view-more-button:hover {
  background-color: #f5f5f5;
  color: #333;
}

.view-more-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  color: #aaa;
}

.empty-icon {
  font-size: 40px;
  margin-bottom: 10px;
}

/* 活动类型颜色 */
.type-user .timeline-badge {
  background-color: #11cdef;
}

.type-order .timeline-badge {
  background-color: #5e72e4;
}

.type-payment .timeline-badge {
  background-color: #2dce89;
}

.type-withdraw .timeline-badge {
  background-color: #fb6340;
}

.type-product .timeline-badge {
  background-color: #f5365c;
}

.type-agent .timeline-badge {
  background-color: #8965e0;
}

.type-distributor .timeline-badge {
  background-color: #ffd600;
}

.type-system .timeline-badge {
  background-color: #525f7f;
}

.type-commission .timeline-badge {
  background-color: #2dce89;
}

@media (max-width: 768px) {
  .timeline-item {
    padding-left: 40px;
  }
  
  .timeline:before {
    left: 20px;
  }
  
  .timeline-badge {
    width: 40px;
    height: 40px;
    line-height: 40px;
  }
  
  .timeline-badge i {
    font-size: 16px;
  }
}
</style> 
 